<%@include file="/WEB-INF/templates/configuration.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ page session="true" %>
<%@ page import="java.util.ArrayList" %>
<%@page import="nl.hva.web.mvcwebapp.models.AssignmentAnswer"%>
<%
HttpSession session_user = request.getSession(true);
    String firstname = (String) session_user.getAttribute("user_firstname");
    int user_id = 0;
    int user_type=0;
    if(firstname!=null){
        user_id = Integer.valueOf("" + session_user.getAttribute("user_id"));
        user_type = Integer.valueOf("" + session_user.getAttribute("user_type"));
    }

%>
<script>
   function disp_confirm()
{
var r=confirm("You are about to submit your Test. Are you sure?")
if (r==true)
  {
  document.submitform.submit();
  }
else
  {
  }
} 
   
</script>
<h2 class="left">Test: ${test.testname}: </h2>
<%if (user_type == 2 || user_type == 3) {%>
<a class="left icon edit" title="edit" href="${url}chapter/assignments/admin?testid=${test.testid}" class="edit">edit</a>
<%}%>
<% 
int i=0;
boolean checked = false;
if(session.getAttribute("filledanswers") != null){
%>
<div class="alert alert-error">
    <p><strong>Be aware,</strong> Not all the Questions are filled in!</p>
</div>

<%
}

%>

<c:choose>
    <c:when test="${amountassignments != 0}">
        <br>
        <br>
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <form id="nieuweCursus" name="submitform" action="assignments" method="post" style="float:left;">
            <input type="hidden" name="testid" value="${test.testid}"/>
            <table class="zebra">
                <tr>
                    <th style="width:100px"> 
                    <strong>Question</strong>
                    </th>
                    <th style="width:300px">
                        <strong>Answers</strong>
                    </th>
                </tr>
                <c:forEach var="tempAssignment" items="${assignments}">
                    <% i++; %>
                    <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                    <tr>
                        <td style="width:300px">
                            <input type="hidden" name="assignment" value="<%=i%>"/>
                            <input type="hidden" name="assignment_<%=i%>" value="${tempAssignment.assignmentid}"/>
                            <div class="description"><strong><%=i%>.</strong> ${tempAssignment.assignmentquestion}</div>
                        </td>
                        <td>
                            <% String box = "";%>
                            <ul class="questions">
                                <c:forEach var="tempAnswer" items="${tempAssignment.assignmentsanswer}">
                                    <% 
                                    if(session.getAttribute("filledanswers") != null){
                                        ArrayList filledanswers = (ArrayList) session.getAttribute("filledanswers");
                                      if(filledanswers.isEmpty() == false){
                                            if(filledanswers.size() > 0){
                                                checked=false;
                                            for(int j=0; j < filledanswers.size(); j++){
                                                AssignmentAnswer answer = (AssignmentAnswer) pageContext.getAttribute("tempAnswer");
                                                int filledanswerid = Integer.parseInt(filledanswers.get(j).toString());
                                                int answerid = answer.getAnswerid();
                                                if(filledanswerid == answerid){
                                                    checked=true;
                                                }
                                              }
                                            }
                                        }
                                     }
                                    %>

                                    <c:choose>
                                        <c:when test="${tempAssignment.assignmenttype == 0}">
                                            <% box = "checkbox";%>
                                        </c:when>
                                        <c:otherwise>
                                            <% box = "radio";%>
                                        </c:otherwise>
                                    </c:choose>

                                    <li><c:choose>
                                            <c:when test="${tempAnswer.answerright == 1}">
                                                
                                            </c:when>
                                            <c:otherwise>

                                            </c:otherwise>
                                        </c:choose><input class=answer-radio style=width:auto type="<%=box%>" <%=(checked)?"checked":""%> name="answer_<%=i%>" value="${tempAnswer.answerid}" /> ${tempAnswer.answername}</li>
                                    </c:forEach>
                            </ul>
                        </td>
                    </tr>     
                </c:forEach> 
                <% 
                    session.setAttribute("filledanswers", null);
                %>
            </table>
            <input class="submit button" type="button" onclick="disp_confirm()" value="Submit Test"> 
        </form>
    </c:when>
    <c:otherwise>
        <!-- Als er geen cursussen zijn, wordt deze melding getoond -->
        There are no chapters found.
    </c:otherwise>
</c:choose>
<br class="clearfix"/>
<p>
    <a class="button" href="${url}index">Back to index</a>
    <a class="button" href="javascript:javascript:history.go(-1)">Back to Test overview</a>
</p>